package com.xiong.dateutils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/8/4 11:15
 * @since JDK 1.8
 */
public class DateUtils {

    public static void main(String[] args) {
        Date date=IntToDateFormartHour(72);
        String s=dateFormarString(date,"yyyy-MM-dd HH:mm:ss");
        System.out.println(s);
    }

    public static Date IntToDateFormartHour(Integer num){
        String s=String.valueOf(num);
        Long n=Long.parseLong(s);
        n=n*3600*1000;
        return dateTimeAddLong(n);
    }

    private static Date dateTimeAddLong(Long num){
        Date now=new Date();
        Long num1=now.getTime();
        num1=num1+num;
        now.setTime(num1);
        return now;
    }




    /**
     * 将字符串转为时间
     * @param str
     * @param formart
     * @return
     */
    public static Date getDateByString(String str,String formart){
        SimpleDateFormat simpleDateFormat=getSimpleDateFormatBy(formart);
        return getDateByString(str,simpleDateFormat);
    }

    /**
     * 时间格式转化
     * @param date
     * @param formart
     * @return
     */
    public static Date dateFormartConver(Date date,String formart){
       SimpleDateFormat simpleDateFormat=getSimpleDateFormatBy(formart);
       String dateStr=getDateToString(date,simpleDateFormat);
       return getDateByString(dateStr,simpleDateFormat);
    }

    public static String dateFormarString(Date date,String formart){
        SimpleDateFormat simpleDateFormat=getSimpleDateFormatBy(formart);
        String dateStr=getDateToString(date,simpleDateFormat);
        return dateStr;
    }

    private static Date getDateByString(String str,SimpleDateFormat simpleDateFormat){
        try {
           return simpleDateFormat.parse(str);
        }catch (ParseException e){
            e.printStackTrace();
        }finally {
            simpleDateFormat=null;
        }
        return new Date();
    }


    private static String getDateToString(Date date,SimpleDateFormat simpleDateFormat){
        return simpleDateFormat.format(date);
    }


    private static SimpleDateFormat getSimpleDateFormatBy(String formart){
        SimpleDateFormat dateFormat=new SimpleDateFormat(formart);
        return dateFormat;
    }
}
